<?php
namespace app\common\model;


use app\extra\BaseModel;
use app\extra\DBCont;

class BagMan extends BaseModel
{
    protected $insert = ['code'];
    public function setCodeAttr()
    {
        do{
            $code = genRandomCode(8, 1|4);
            $id = $this->where('code', '=', $code)->value('id');
        }while($id);
        return $code;
    }
    public function getMatchIdAttr($value){
        if (empty($value)){
            return null;
        }
        return [
            'key' => $value,
            'value' => model('Match')->where('id', $value)->value('name')
        ];
    }
    /**
     * 添加额外参数
     * @param $item
     */
    public function setAddedField(&$item){
        $team_id = model('Team')->where('share_id', '=', $item['id'])->column('id');
        if (empty($team_id)){
            // 支付人数
            $item['people_number'] = 0;
            // 支付金额
            $item['sum_amount'] = 0;
        } else {
            $item['people_number'] = model('Member')->whereIn('team_id', $team_id)->count();
            $item['sum_amount'] = model('Member')->whereIn('team_id', $team_id)
                ->where('way', '=', DBCont::WECHAT)
                ->where('pay_state', '=', DBCont::SUCCESS)
                ->sum('amount');
        }
    }
}